package com.lanhai.longheng.report;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import com.lanhai.longheng.beans.AgvBean;
import com.lanhai.longheng.beans.AgvDataBean;
import com.lanhai.longheng.dispatch.AgvCmdMap;
import com.lanhai.longheng.utils.Tools;

/**
 * 记录每条指令时小车的状态（高频）
 * @author zhengwei
 *
 */
public class AgvStateMap {
	
	static Map<Integer,AgvStateBean> agvMap=Collections.synchronizedMap(new HashMap<Integer,AgvStateBean>());

	public static   void saveState (AgvDataBean bean,AgvBean agv)
	{
		try {
		AgvStateBean ab=new AgvStateBean();
		ab.setAgvid(bean.getAgvId());
		ab.setLastTime(System.currentTimeMillis());
		ab.setX(bean.getX());
		ab.setY(bean.getY());
		ab.setCmd(bean.getReceiveType().getCode());
		ab.setNextNode(agv.getNextNode());
		ab.setSpecialline(agv.getSpecialline());
		ab.setProcess( agv.getNextProcess()); 
		
		ab.setUpHasTrans(agv.isUpHasTrans()?1:0 );
		ab.setDownHasTrans(agv.isDownHasTrans()?1:0);
		ab.setFinishTrans(agv.isFinishTrans()?1:0);
		
		if(AgvCmdMap.isTransing(agv.getAgvId())){
			ab.setTransing(1);
		}else {
			ab.setTransing(0);
		}
		
		agvMap.put(ab.getAgvid(), ab);
		}catch(Exception e) {
			e.printStackTrace();
		}
	}
	
	public static synchronized List<AgvStateBean> getAll(){
		List<AgvStateBean> re=new ArrayList<AgvStateBean>(); 
		for(Entry<Integer,AgvStateBean> entry: agvMap.entrySet()) {
			AgvStateBean mp=entry.getValue(); 
				AgvStateBean ab=new AgvStateBean();
				ab.setAgvid(mp.getAgvid());
				ab.setLastTime( mp.getLastTime());
				ab.setX(mp.getX());
				ab.setY(mp.getY());
				ab.setCmd(mp.getCmd()); 
				ab.setNextNode(mp.getNextNode());
				ab.setSpecialline(mp.getSpecialline());
				ab.setProcess(Tools.getProcess(mp.getProcess())); 
				ab.setFinishTrans(mp.getFinishTrans());
				ab.setUpHasTrans(mp.getUpHasTrans());
				ab.setDownHasTrans(mp.getDownHasTrans());
				ab.setTransing(mp.getTransing());
				re.add(ab);
		}
		
		return re;
	}
}
